package com.bestcem.xm.om.grpc.service.converter;

import com.bestcem.xm.om.entity.OmPrincipal;
import com.bestcem.xm.om.entity.OmRole;
import com.bestcem.xm.om.entity.OmUser;
import com.bestcem.xm.om.entity.OmUserIndustry;
import com.bestcem.xm.om.grpc.v1.enums.OmRoleTypeEnum;
import com.bestcem.xm.om.grpc.v1.enums.OmUserStatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

/**
 * 描述信息
 *
 * @author xuan.yang <xuan.yang@idiaoyan.com>
 * @version v1.0
 * @date 2021/12/30 11:09
 */
@Slf4j
public class OmUserConverter {
    private OmUserConverter() {
    }

    public static OmUser convertToOmUser(com.bestcem.xm.om.grpc.v1.services.OmUser grpcUser) {
        OmUser user = new OmUser();
        user.setId(grpcUser.getId());
        user.setPhone(grpcUser.getPhone());
        user.setEmail(grpcUser.getEmail());
        user.setRealName(grpcUser.getName());
        user.setUsername(grpcUser.getUsername());
        user.setRoleId(grpcUser.getRoleId());
        if (OmUserStatusEnum.OmUserStatus.ACTIVATE_VALUE == grpcUser.getStatusValue()) {
            user.setStatus(com.bestcem.xm.om.enums.OmUserStatusEnum.ENABLE.getIndex());
        } else if (OmUserStatusEnum.OmUserStatus.INVALIDATE_VALUE == grpcUser.getStatusValue()) {
            user.setStatus(com.bestcem.xm.om.enums.OmUserStatusEnum.DISABLE.getIndex());
        }
        return user;
    }

    public static com.bestcem.xm.om.grpc.v1.services.OmUser convertToGrpcUser(OmUser omUser) {
        com.bestcem.xm.om.grpc.v1.services.OmUser.Builder builder = com.bestcem.xm.om.grpc.v1.services.OmUser.newBuilder();
        builder.setPhone(omUser.getPhone())
                .setName(omUser.getRealName())
                .setUsername(omUser.getUsername())
                .setId(omUser.getId())
                .setRoleId(omUser.getRoleId());
        if (StringUtils.isNotBlank(omUser.getEmail())) {
            builder.setEmail(omUser.getEmail());
        }
        if (com.bestcem.xm.om.enums.OmUserStatusEnum.ENABLE.getIndex().equals(omUser.getStatus())) {
            builder.setStatus(OmUserStatusEnum.OmUserStatus.ACTIVATE);
        } else if (com.bestcem.xm.om.enums.OmUserStatusEnum.DISABLE.getIndex().equals(omUser.getStatus())) {
            builder.setStatus(OmUserStatusEnum.OmUserStatus.INVALIDATE);
        } else {
            builder.setStatus(OmUserStatusEnum.OmUserStatus.UNRECOGNIZED);
        }
        return builder.build();
    }

    public static com.bestcem.xm.om.grpc.v1.services.OmUserIndustry convertToGrpcIndustry(OmUserIndustry omUserIndustry) {
        if (Objects.isNull(omUserIndustry)) {
            return null;
        }
        com.bestcem.xm.om.grpc.v1.services.OmUserIndustry.Builder builder = com.bestcem.xm.om.grpc.v1.services.OmUserIndustry.newBuilder();
        builder.setId(omUserIndustry.getId())
                .setIndustryName(omUserIndustry.getIndustryName())
                .setUserId(omUserIndustry.getOmUserId())
                .setIndustryId(omUserIndustry.getIndustryId());
        return builder.build();
    }

    public static OmUserIndustry convertToIndustry(com.bestcem.xm.om.grpc.v1.services.OmUserIndustry grpcIndustry) {
        OmUserIndustry omUserIndustry = new OmUserIndustry();
        omUserIndustry.setId(grpcIndustry.getId());
        omUserIndustry.setIndustryName(grpcIndustry.getIndustryName());
        omUserIndustry.setOmUserId(grpcIndustry.getUserId());
        omUserIndustry.setIndustryId(grpcIndustry.getIndustryId());
        return omUserIndustry;
    }

    public static OmRole convertToOmRole(com.bestcem.xm.om.grpc.v1.services.OmRole grpcRole) {
        OmRole role = new OmRole();
        role.setId(grpcRole.getId());
        role.setTitle(grpcRole.getTitle());
        role.setContent(grpcRole.getContent());
        role.setPermissions(grpcRole.getPermissions());
        if (OmRoleTypeEnum.OmRoleType.SUPERAMDIN_VALUE == grpcRole.getTypeValue()) {
            role.setType(com.bestcem.xm.om.enums.OmRoleTypeEnum.SUPER_ADMIN.getIndex());
        } else if (OmRoleTypeEnum.OmRoleType.USER_VALUE == grpcRole.getTypeValue()) {
            role.setType(com.bestcem.xm.om.enums.OmRoleTypeEnum.COMMON_USER.getIndex());
        } else {
            if (log.isInfoEnabled()) {
                log.info("[om]grpc角色类型不合法,值为{}", grpcRole.getTypeValue());
            }
        }
        return role;
    }

    public static com.bestcem.xm.om.grpc.v1.services.OmRole convertToGrpcRole(OmRole omRole) {
        if (Objects.isNull(omRole)) {
            return null;
        }
        com.bestcem.xm.om.grpc.v1.services.OmRole.Builder builder = com.bestcem.xm.om.grpc.v1.services.OmRole.newBuilder();
        builder.setId(omRole.getId())
                .setTitle(omRole.getTitle())
                .setContent(omRole.getContent())
                .setPermissions(omRole.getPermissions())
                .setTypeValue(omRole.getType());
        return builder.build();
    }

    public static OmPrincipal convertToPrincipal(com.bestcem.xm.om.grpc.v1.services.OmPrincipal grpcPrincipal) {
        OmPrincipal omPrincipal = new OmPrincipal();
        omPrincipal.setId(grpcPrincipal.getId());
        omPrincipal.setOrgId(grpcPrincipal.getOrgId());
        omPrincipal.setOmUserId(grpcPrincipal.getOmuserId());
        omPrincipal.setType(grpcPrincipal.getTypeValue());
        omPrincipal.setName(grpcPrincipal.getName());
        omPrincipal.setUserName(omPrincipal.getUserName());
        return omPrincipal;
    }

    public static com.bestcem.xm.om.grpc.v1.services.OmPrincipal convertToGrpcPrincipal(OmPrincipal principal) {
        com.bestcem.xm.om.grpc.v1.services.OmPrincipal.Builder builder = com.bestcem.xm.om.grpc.v1.services.OmPrincipal.newBuilder();
        builder.setId(principal.getId())
                .setOrgId(principal.getOrgId())
                .setOmuserId(principal.getOmUserId())
                .setTypeValue(principal.getType())
                .setName(principal.getName())
                .setUsername(principal.getUserName());
        return builder.build();
    }

}
